class PartnersController < ApplicationController
  before_action :set_partner, only: [:show, :edit, :update, :destroy]
  before_action :set_page_title

  # GET /partners
  # GET /partners.json
  def index
    @partners = Partner.where(build_search(search_params)).order(code: :asc).page(params[:page])
  end

  # GET /partners/1
  # GET /partners/1.json
  def show
    redirect_to edit_partner_path(@partner)
  end

  # GET /partners/new
  def new
    # @partner = Partner.new
    @partner = Partner.send(set_type.pluralize).new

    # 2.times do |partner|
    #   @partner.contacts.build
    # end

  end

  # GET /partners/1/edit
  def edit

  end

  # POST /partners
  # POST /partners.json
  def create
    # @partner = Partner.new(partner_params)
    @partner = Partner.send(set_type.pluralize).new(partner_params)


    respond_to do |format|
      if @partner.save
        format.html { redirect_to edit_partner_path(@partner), success: '创建成功！' }
        format.json { render :show, status: :created, location: @partner }
      else
        format.html { render :new }
        format.json { render json: @partner.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /partners/1
  # PATCH/PUT /partners/1.json
  def update
    respond_to do |format|
      if @partner.update(partner_params)
        format.html { redirect_to @partner, success: '更新成功！' }
        format.json { render :show, status: :ok, location: @partner }
      else
        format.html { render :edit }
        format.json { render json: @partner.errors, status: :unprocessable_entity }
      end

    end
  end

  # DELETE /partners/1
  # DELETE /partners/1.json
  def destroy
    @partner.destroy
    respond_to do |format|
      format.html { redirect_to partners_url, notice: 'Partner was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_partner
      @partner = Partner.find(params[:id])
    end

    # Only allow a list of trusted parameters through.
    def partner_params
      # params.require(:partner).permit(:code, :name, :shortname, :partner_type, :partner_category_id, :payable_partner_id, :deputy_depart_id, :sales_id, :address, :tel, :fax, :tax_no, :bank, :bank_account, :bank_address,
      #   contacts_attributes: [:id,:address,:name,:cell,:comment,:is_primary])
      params.require(set_type.to_sym).permit(:type,:code, :name, :shortname, :partner_type, :partner_category_id, :payable_partner_id, :deputy_depart_id, :sales_id, :address, :tel, :fax, :tax_no, :bank, :bank_account, :bank_address,
                                      contacts_attributes: [:id,:address,:name,:cell,:comment,:is_primary])
    end

    # def sanitize_partner_params
    #   params[:type] = params[:type].to_i
    # end

    def set_page_title
      @page_title = "往来单位"
    end

    def search_params
      if params[:partner].present?
        partner_params.permit(:partner_category_id)
      end
    end

    def set_type
      case params[:type]
      # when nil
      #   'partner'
      when 'Customer'
        "customer"
      when 'Supplier'
        "supplier"
      end
    end
end
